package com.derbysoft.lowcostadapter.dswitch2.translator.impl;

import com.derbysoft.lowcostadapter.commons.dto.LoginDetails;
import com.derbysoft.lowcostadapter.commons.exception.IdentificationNotFoundException;
import com.derbysoft.lowcostadapter.commons.model.ProviderIdentification;
import com.derbysoft.lowcostadapter.commons.redis.service.ProviderIdentificationCacheService;
import com.derbysoft.lowcostadapter.commons.repository.HotelRepository;
import com.derbysoft.lowcostadapter.commons.repository.ProviderIdentificationRepository;
import com.derbysoft.remote.dswitch.generic.constants.AgeQualifyingType;
import com.derbysoft.remote.dswitch.hotel.dto.GuestCountDTO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public abstract class AbstractRQTranslator {
    private static final String GTA = "GTA";
    private static final String GTA_PASSPORT = "BB06B537FDA408849E0C465E0919F6B003BD";
    @Autowired
    protected HotelRepository hotelRepository;

    @Autowired
    protected ProviderIdentificationCacheService providerIdentificationCacheService;

    @Autowired
    protected ProviderIdentificationRepository providerIdentificationRepository;

    protected LoginDetails createLoginDetails(String channelCode) {
        LoginDetails loginDetails = new LoginDetails();
        ProviderIdentification identification = loadByChannel(channelCode);
        loginDetails.setLogin(identification.getUserName());
        loginDetails.setPassword(identification.getPassWd());
        loginDetails.setAgentReference(identification.getAgentReference());
        return loginDetails;
    }

    protected ProviderIdentification loadByChannel(String channelCode) {
        ProviderIdentification identification = providerIdentificationCacheService.get(StringUtils.equals(channelCode, GTA_PASSPORT) ? GTA : channelCode);
        if (identification == null) {
            identification = providerIdentificationRepository.loadByChannel(StringUtils.equals(channelCode, GTA_PASSPORT) ? GTA : channelCode);
        }
        if (identification == null) {
            throw new IdentificationNotFoundException(channelCode);
        }
        return identification;
    }

    public void setHotelRepository(HotelRepository hotelRepository) {
        this.hotelRepository = hotelRepository;
    }

    protected int getGuestCount(List<GuestCountDTO> guestCounts, AgeQualifyingType adult) {
        for (GuestCountDTO guestCount : guestCounts) {
            if (adult == guestCount.getAgeQualifyingType()) {
                return guestCount.getCount();
            }
        }
        return 0;
    }

    public void setProviderIdentificationCacheService(ProviderIdentificationCacheService providerIdentificationCacheService) {
        this.providerIdentificationCacheService = providerIdentificationCacheService;
    }

    public void setProviderIdentificationRepository(ProviderIdentificationRepository providerIdentificationRepository) {
        this.providerIdentificationRepository = providerIdentificationRepository;
    }
}
